#include "WebConnection.h"

namespace Gw2API{

WebConnection::WebConnection(void)
{
	languages[0] = "lang=de";
	languages[1] = "lang=fr";
	languages[2] = "lang=en";
	lang = GW2API_LANG_DE;
}


WebConnection::~WebConnection(void)
{
	
}


void WebConnection::startSession(){
	openHandle = InternetOpen(TEXT(APP_ID), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	sessionHandle = InternetConnect(openHandle, TEXT(API_SERVER), INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
}


void WebConnection::endSession(){
	InternetCloseHandle(sessionHandle);
	InternetCloseHandle(openHandle);
}


bool WebConnection::setLanguage(int lang){
	if(lang >= 0 && lang < 3){
		this->lang = lang;
		return true;
	}
	else{
		this->lang = GW2API_LANG_DE;
		return false;
	}
}


LPCWSTR WebConnection::convertToWideChar(std::string toConvert){
	int len;
	int slength = toConvert.length()+ 1;
    len = MultiByteToWideChar(CP_ACP, 0, toConvert.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, toConvert.c_str(), slength, buf, len);
	return buf;
}


std::string WebConnection::recieveServerResponse(HINTERNET requestHandle){
	char recv;
	DWORD bytes;
	std::string result = "";
	bool utf8char = false;
	LPVOID buffer;

	buffer = 0;
	bytes = 0;
retry:
	if(!HttpQueryInfo(requestHandle, HTTP_QUERY_STATUS_CODE, buffer, &bytes, NULL)){
		buffer = malloc(bytes);
		goto retry;
	}
	if(atoi((char*)buffer) != 200){
		free(buffer);
		return result;
	}

	while(InternetReadFile(requestHandle, &recv, 1, &bytes)){
		if(bytes != 1)
			break;
		if(recv == -61 && !utf8char){
			utf8char = true;
			continue;
		}
		if(utf8char){
			utf8char = false;
			switch(recv){
			case -92:
				result += "ae";
				break;
			case -74:
				result += "oe";
				break;
			case -68:
				result += "ue";
				break;
			}
			continue;
		}
		result += recv;
	}
	InternetCloseHandle(requestHandle);
	endSession();
	return result;
}


std::string WebConnection::sendServerRequest(std::string *request){
	if(request == 0)
		return NULL;
	HINTERNET requestHandle;
	startSession();
	requestHandle = HttpOpenRequestA(sessionHandle, NULL, request->c_str(), NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
	HttpSendRequest(requestHandle, NULL, 0, NULL, 0);
	return recieveServerResponse(requestHandle);
}


std::string WebConnection::WorldMappingRequest(){
	std::string request = WORLD_MAPPING_API + languages[lang];
	return sendServerRequest(&request);	
}


std::string WebConnection::MapMappingRequest(){
	std::string request = MAP_MAPPING_API + languages[lang];
	return sendServerRequest(&request);
}


std::string WebConnection::EventdMappingRequest(){
	std::string request = EVENT_MAPPING_API + languages[lang];
	return sendServerRequest(&request);
}


std::string WebConnection::EventStateRequest(std::string *world_id, std::string *map_id, std::string *event_id){
	if(world_id == 0)
		return NULL;
	std::string request = DYNAMIC_EVENTS_API + *world_id;
	if(map_id != 0)
		request += "&map_id=" + *map_id;
	if(event_id != 0)
		request += "&event_id=" + *event_id;
	return sendServerRequest(&request);
}


std::string WebConnection::MatchMappingRequest(){
	std::string request = WVW_MATCHES;
	return sendServerRequest(&request);
}


std::string WebConnection::MatchDetailsRequest(std::string *match_id){
	if(match_id == 0)
		return NULL;
	std::string request = WVW_MATCH_DETAILS + *match_id;
	return sendServerRequest(&request);
}


std::string WebConnection::WVWObjectiveNamesRequest(){
	std::string request = WVW_OBJECTIVE_NAMES + languages[lang];
	return sendServerRequest(&request);
}


std::string WebConnection::ItemDetailsRequest(std::string *id){
	std::string request = ITEM_DETAILS + *id + "&" + languages[lang];
	return sendServerRequest(&request);
}

	
std::string WebConnection::RecipeListRequest(){
	std::string request = ITEMS_API;
	return sendServerRequest(&request);
}


std::string WebConnection::RecipeDetailsRequest(std::string *id){
	std::string request = RECIPE_DETAILS + *id + "&" + languages[lang];
	return sendServerRequest(&request);
}


std::string WebConnection::GuildAPIRequest(std::string *id, std::string *name){
	std::string request = GUILD_API;
	if(id == 0 && name == 0)
		return "";
	if(id == 0)
		request += "guild_id=" + *id;
	else
		request += "guild_name=" + *name;
	return sendServerRequest(&request);
}


std::string WebConnection::ApiBuildRequest(){
	std::string request = API_BUILD;
	return sendServerRequest(&request);
}

	
std::string WebConnection::ColorRequest(){
	std::string request = COLORS_API + languages[lang];
	return sendServerRequest(&request);
}


std::string WebConnection::ItemListRequest(){
	std::string request = ITEMS_API;
	return sendServerRequest(&request);
}

};